package com.fingard.dsp.web.page.m;

import java.net.URLDecoder;
import java.sql.Connection;
import java.util.HashMap;

import com.fingard.FGBiz;
import com.fingard.common.utils.json.JSONUtils;
import com.fingard.db.ConnSet;
import com.fingard.db.JDBCHelper;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.DSPSet;
import com.fingard.dsp.web.page.PageBase;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;

public class Qry extends PageBase {
	@Override
    public void pageLoad() throws Exception {
        httpContext.response.isRtnBytes = false;
        httpContext.response.setContentType("txt", "");
        StringBuilder sbJson = new StringBuilder();
        String respCode = "";
        String respInfo = "";
        Connection conn = null;
        try {

            String reqJson = httpContext.request.httpReceiver.getReqBodyContent();
            HashMap<String, String> reqParams = JSONUtils.json2Map(reqJson);
            
            String dbfile="";
            String sqltext = "";
            if (reqParams.containsKey("dbfile")) {
            	dbfile = URLDecoder.decode(reqParams.get("dbfile"), httpContext.request.httpReceiver.getCharset());
            }
            if (reqParams.containsKey("sqltext")) {
            	sqltext = URLDecoder.decode(reqParams.get("sqltext"), httpContext.request.httpReceiver.getCharset());
            }

            if (StringHelper.hasAnyChar(dbfile) && StringHelper.hasAnyChar(sqltext)) {
            	dbfile = DSPSet.replaceFilePath(dbfile);
            	if(FileHelper.existFile(dbfile)){
	            	ConnSet connSet = new ConnSet();
	            	connSet.dbDriver = "org.sqlite.JDBC";
	            	connSet.dbUrl = "jdbc:sqlite:" + dbfile;
	            	conn = connSet.getConnection();
	            	sbJson = JDBCHelper.execQueryToJson(conn, sqltext, null);
	            	respCode = "0";
            	}else{
            		respCode = "-1";
                	respInfo = "本地文件"+dbfile+"不存在";
            	}
            	
            }
            if(StringHelper.isNullOrEmpty(dbfile)){
            	respCode = "-1";
            	respInfo = "本地文件为空；";
            }
            if(StringHelper.isNullOrEmpty(sqltext)){
            	respCode = "-1";
            	respInfo += "执行语句为空；";
            }
            
        } catch (Exception ex) {
            respCode = "-1";
            respInfo = LogHelper.getStackTraceFirstLine(ex);
            FGBiz.limitMsg.exception(this.getClass().getSimpleName(), ex);
        } finally {
        	try{
        		if (conn != null) {
                    conn.close();
                }
        	}catch(Exception e){
        		FGBiz.limitMsg.exception(this.getClass().getSimpleName(), e);
        	}
            
        }

        if(!"0".equals(respCode)){
        	sbJson.append("{");
            sbJson.append("\"respCode\":" + "\"" + respCode + "\",");
            sbJson.append("\"respInfo\":" + "\"" + respInfo + "\"");
            sbJson.append("}");
        }
        
        httpContext.response.respMsg = sbJson.toString();
	}
}
